<?php

// +----------------------------------------------------------------------
// | app信息
// +----------------------------------------------------------------------
// | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
// +----------------------------------------------------------------------
// | Author: 微尘 <yicmf@qq.com>
// +----------------------------------------------------------------------

namespace app\controller\api\v3;

use app\model\app\Version as VersionModel;
use app\model\app\Device as DeviceModel;
use app\event\app\Device as DeviceEvent;
use think\response\Json;
use wechat\official_account\Template as TemplateOfficial;

class App extends Common
{

    /**
     * 获取APP配置
     * @return \think\Response|Json
     */
    public function config()
    {

        $data['register']['captcha'] = config('ucenter.safe_captcha');
        $data['register']['account'] = config('ucenter.register_account');
        $data['is_examine'] = $this->is_examine;
        if ($this->is_examine) {
            $data['vip_name'] = '读者';
            $data['vip_names'] = ['新手入门', '初级读者', '中级读者', '高级读者'];
            // 强制实名
            $data['is_idcard'] = 0;
            // 提示储存权限
            $data['is_show_storage'] = 1;
        } else {
            $data['vip_name'] = '合伙人';
            $data['vip_names'] = ['新手入门', '初级合伙人', '中级合伙人', '高级合伙人'];
            // 强制实名
            $data['is_idcard'] = 1;
            // 提示储存权限
            $data['is_show_storage'] = 1;
        }
        $data['name'] = '云副业';
        $data['about']['copyright'] = 'Copyright©2024';
        $data['download_guide'] = '下载引导页';
        $data['service_url'] = '客服链接';
        $data['service_qq'] = '客服链接';

        return $this->successAjax($data);
    }

    /**
     * 获取最新版本信息
     */
    public function version()
    {

        $platform = $this->request->param('platform', 'android');
        $channel = $this->request->param('channel', 'h5');
        $version = VersionModel::where('status', 1)->where('version_code', $this->request->param('version_code', ''))->find();
        if ($version) {
            if ($this->is_examine == 0) {
                if (1 != $version['is_examine']) {
                    // 非上架版本查询更新
                    if ($this->user && $this->user['id'] == 36)
                    {
                        $version = VersionModel::where('status', 1)->where('need_version_code', '<=', $version['version_code'])->order('id DESC')
                            ->where('platform', $platform)
//                    ->where('is_silence',1)
                            ->find();
                    }else{
                        $version = VersionModel::where('status', 1)->where('need_version_code', '<=', $version['version_code'])->order('id DESC')
                            ->where('platform', $platform)
//                    ->where('is_silence',1)
                            ->where('is_test',0)
                            ->find();
                    }

                }
            }
//            if (in_array($channel,config('app_store.store_hot'))|| 1 != $version['is_examine'])
//            {
//                // 非上架版本查询静默更新
//                $version = VersionModel::where('status', 1)->where('need_version_code','<=',$version['version_code'])->order('id DESC')
//                    ->where('platform', $platform)
//                    ->where('is_silence',1)
//                    ->find();
//            }

            //  if (1 ||$version['is_issue'] != 1)
            // {
            //     // 非上架版本查询静默更新
            //     $version = VersionModel::where('status', 1)->where('is_silence',1)->order('version_code DESC')->where('platform', $platform)->find();
            // }
        } else {
            // 未查询到版本返回最新
            $version = VersionModel::where('status', 1)
                ->where('is_silence', 0)
                ->where('is_examine', 0)
                ->where('is_test',0)
                ->order('version_code DESC')
                ->where('platform', $platform)->find();
        }
        if ($version) {
            $rep['code'] = 0;
//            $data['is_silence'] =  $version['is_silence']; // 是否静默更新 0代表否 1代表是
//            $data['is_force'] = $version['is_force'];; //是否强制更新 0代表否 1代表是
//            $data['package_type'] = 1; //0是整包升级（apk或者appstore或者安卓应用市场） 1是wgt升级
//            $data['is_issue'] = 1; //是否发行  0否 1是 为了控制上架应用市场审核时不能弹出热更新框
//            $data['version_code'] =  $version['version_code']; //版本号 最重要的manifest里的版本号 （检查更新主要以服务器返回的version_number版本号是否大于当前app的版本号来实现是否更新）
//            $data['version_name'] = $version['version_name'];// 版本名称 manifest里的版本名称
//            $data['describe'] = $version['log'];
//            $data['wgt_url'] =  $version['wgt_url'];//apk下载地址或者应用市场地址  安卓应用市场 market://details?id=xxxx 苹果store itms-apps://itunes.apple.com/cn/app/xxxxxx
//            $data['pkg_url'] =  $version['pkg_url'];//wgt包下载地址
//            $data['title'] = $version['title'];
            $rep['data'] = $version->hidden(['create_time', 'update_time', 'delete_time', 'channel', 'appid', 'id', 'status', 'system_version'])->toArray();
        } else {
            $rep['code'] = 1;
            $rep['message'] = '暂无新版本';
        }
        return $this->autoAjax($rep);
    }

    public function test()
    {
//
//
//        $oa_data = [
//            'time6' => ['value' => time_format(), 'color' => '#000000'],
//            'character_string10' => ['value' =>302, 'color' => '#000000'],
//            'time3' => ['value' => time_format(), 'color' => '#000000'],
//            'character_string4' => ['value' =>'/dasd/sd/asda/as', 'color' => '#000000'],
//            'thing2' => ['value' =>'错误信息', 'color' => '#000000'],
//        ];
//        $account['token'] = 'yunfuye1';
//        $account['encoding_aes_key'] = 'BkrUUPz9IgfERTRXmwiIf0RSir4ONBku64EufsZ9EE2';
//        $account['app_secret'] = 'cbdd44c7d119c8b4dd51b74c3030aa75';
//        $account['app_id'] = 'wxd4cc6bfc61b75cc5';
//        $oa = new TemplateOfficial();
//        $oa->initAccount($account);
//        // 微尘
//        $re = $oa->send('oAAIg6w_C0wy7_rmcb3VahTohD6s', '2sxrOmUnm8f0doRH8qic6qZ4RjUjOG2aiBbnNNn3VrY', $oa_data, '');

//
//        $oa_data = [
//            'thing27' => ['value' =>'电商商品名字', 'color' => '#000000'],
//            'time28' => ['value' => time_format(), 'color' => '#000000'],
//            'phrase9' => ['value' =>'电商优惠', 'color' => '#000000'],
//        ];
//        $account['token'] = 'yunfuye1';
//        $account['encoding_aes_key'] = 'BkrUUPz9IgfERTRXmwiIf0RSir4ONBku64EufsZ9EE2';
//        $account['app_secret'] = 'cbdd44c7d119c8b4dd51b74c3030aa75';
//        $account['app_id'] = 'wxd4cc6bfc61b75cc5';
//        $oa = new TemplateOfficial();
//        $oa->initAccount($account);
//        // 微尘
//        $re = $oa->send('oAAIg6w_C0wy7_rmcb3VahTohD6s', 'Qa5PspnbrF_0aZ02UePEdmycBdftWM_qkeKRUp4l-4E', $oa_data, '');
//


        $event = new DeviceEvent();


        $param['type'] = 'auth';
        $param['title'] = '你好啊';
        $param['relation_id'] = 1;
        $param['content'] = '收到了么？' . time_format();

//        $data = $event->pushToByClientId($param, '4fa88d6ad6009c829e49f3215afd9197');
//        dump($data);
//        exit();
        $user = \app\model\ucenter\User::find(16006);
        $data = $event->pushToSingleUserTest($param, $user);
        dump($data);
    }

    /**
     * 更新设备信息
     */
    public function device()
    {
        if ($this->request->isPost()) {
            $event = new DeviceEvent();
            $data = $event->update($this->request->post(), $this->user);
        } else {
            $data['code'] = 0;
            $data['message'] = 'ok';
        }
        return $this->autoAjax($data);
    }

    /**
     * 退出设备信息
     */
    public function logout()
    {
        if ($this->request->isPost()) {
            $event = new DeviceEvent();
            $data = $event->logout($this->request->post(), $this->user);
        } else {
            $data = [];
            $data['code'] = 0;
        }
        return $this->autoAjax($data);
    }


}
